home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1992-07-02  |  59KB  |  2,283 lines

  1. /* Keyboard input; editor command loop.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*** For version 19, can simplify this by making interrupt_input 1 on VMS.  */
  21.  
  22. /* Allow config.h to undefine symbols found here.  */
  23. #include <signal.h>
  24.  
  25. #include "config.h"
  26. #include <stdio.h>
  27. #undef NULL
  28. #include "termchar.h"
  29. #include "termopts.h"
  30. #include "termhooks.h"
  31. #include "lisp.h"
  32. #include "macros.h"
  33. #include "window.h"
  34. #include "commands.h"
  35. #include "buffer.h"
  36. #include <setjmp.h>
  37. #include <errno.h>
  38.  
  39. extern int errno;
  40.  
  41. /* Get FIONREAD, if it is available.  */
  42. #ifdef USG
  43. #include <termio.h>
  44. #include <fcntl.h>
  45. #else /* not USG */
  46. #ifndef VMS
  47. #include <sys/ioctl.h>
  48. #endif /* not VMS */
  49. #endif /* not USG */
  50.  
  51. #include "emacssignal.h"
  52.  
  53. /* Allow m- file to inhibit use of FIONREAD.  */
  54. #ifdef BROKEN_FIONREAD
  55. #undef FIONREAD
  56. #endif
  57.  
  58. /* Make all keyboard buffers much bigger when using X windows.  */
  59. #ifdef HAVE_X_WINDOWS
  60. #define BUFFER_SIZE_FACTOR 16
  61. #else
  62. #define BUFFER_SIZE_FACTOR 1
  63. #endif
  64.  
  65. /* Following definition copied from eval.c */
  66.  
  67. struct backtrace
  68.   {
  69.     struct backtrace *next;
  70.     Lisp_Object *function;
  71.     Lisp_Object *args;    /* Points to vector of args. */
  72.     int nargs;        /* length of vector */
  73.            /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
  74.     char evalargs;
  75.   };
  76.  
  77. /* Non-nil disable property on a command means
  78.  do not execute it; call disabled-command-hook's value instead. */
  79. Lisp_Object Qdisabled, Vdisabled_command_hook;
  80.  
  81. int recent_keys_index;    /* Index for storing next element into recent_keys */
  82. int total_keys;        /* Total number of elements stored into recent_keys */
  83. char recent_keys[100];    /* Holds last 100 keystrokes */
  84.  
  85. /* Buffer holding the key that invoked the current command.  */
  86. unsigned char *this_command_keys;
  87. int this_command_key_count;    /* Size in use.  */
  88. int this_command_keys_size;    /* Size allocated.  */
  89.  
  90. extern struct backtrace *backtrace_list;
  91.  
  92. static jmp_buf getcjmp;    /* for longjmp to where kbd input is being done. */
  93.  
  94. int waiting_for_input;    /* True while doing kbd input */
  95.  
  96. /* True while displaying for echoing.   Delays C-g throwing. */
  97. static int echoing;
  98.  
  99. int immediate_quit;    /* Nonzero means C-G should cause immediate error-signal. */
  100.  
  101. int help_char;        /* Character to recognize as the help char.  */
  102.  
  103. Lisp_Object Vhelp_form;    /* Form to execute when help char is typed.  */
  104.  
  105. /* Character that causes a quit.  Normally C-g.  */
  106.  
  107. int quit_char;
  108.  
  109. extern Lisp_Object global_map;
  110.  
  111. /* Current depth in recursive edits.  */
  112.  
  113. int command_loop_level;
  114.  
  115. /* Last input character read as a command.  */
  116.  
  117. int last_command_char;
  118.  
  119. /* Last input character read for any purpose.  */
  120.  
  121. int last_input_char;
  122.  
  123. /* If not -1, a character to be read as the next command input */
  124.  
  125. int unread_command_char;
  126.  
  127. /* Char to use as prefix when a meta character is typed in.
  128.  This is bound on entry to minibuffer in case Esc is changed there.  */
  129.  
  130. int meta_prefix_char;
  131.  
  132. /* Total number of times read_command_char has returned.  */
  133.  
  134. int num_input_chars;
  135.  
  136. /* Auto-save automatically when this many characters have been typed
  137.    since the last time.  */
  138.  
  139. static int auto_save_interval;
  140.  
  141. /* Value of num_input_chars as of last auto save.  */
  142.  
  143. int last_auto_save;
  144.  
  145. /* Last command executed by the editor command loop, not counting
  146.    commands that set the prefix argument.  */
  147.  
  148. Lisp_Object last_command;
  149.  
  150. /* The command being executed by the command loop.
  151.    Commands may set this, and the value set will be copied into last_command
  152.    instead of the actual command.  */
  153. Lisp_Object this_command;
  154.  
  155. Lisp_Object Qself_insert_command;
  156. Lisp_Object Qforward_char;
  157. Lisp_Object Qbackward_char;
  158.  
  159. /* read_key_sequence stores here the command definition of the
  160.    key sequence that it reads.  */
  161. Lisp_Object read_key_sequence_cmd;
  162.  
  163. /* Form to evaluate (if non-nil) when Emacs is started */
  164. Lisp_Object Vtop_level;
  165.  
  166. /* User-supplied string to translate input characters through */
  167. Lisp_Object Vkeyboard_translate_table;
  168.  
  169. FILE *dribble;            /* File in which we write all commands we read */
  170.  
  171. /* Nonzero if input is available */
  172. int input_pending;
  173.  
  174. /* Nonzero if should obey 0200 bit in input chars as "Meta" */
  175. int meta_key;
  176.  
  177. extern char *pending_malloc_warning;
  178.  
  179. /* Buffer for pre-read keyboard input */
  180. unsigned char kbd_buffer [256 * BUFFER_SIZE_FACTOR];
  181.  
  182. /* Number of characters available in kbd_buffer.  */
  183. int kbd_count;
  184.  
  185. /* Pointer to next available character in kbd_buffer.  */
  186. unsigned char *kbd_ptr;
  187.  
  188. /* Address (if not 0) of word to zero out
  189.    if a SIGIO interrupt happens */
  190. long *input_available_clear_word;
  191.  
  192. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  193.    Default is 1 if INTERRUPT_INPUT is defined.  */
  194.  
  195. int interrupt_input;
  196.  
  197. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  198.  
  199. int interrupts_deferred;
  200.  
  201. /* nonzero means use ^S/^Q for flow control.  */
  202.  
  203. int flow_control;
  204.  
  205. #ifndef BSD4_1
  206. #define sigfree() sigsetmask (SIGEMPTYMASK)
  207. #define sigholdx(sig) sigsetmask (sigmask (sig))
  208. #define sigblockx(sig) sigblock (sigmask (sig))
  209. #define sigunblockx(sig) sigblock (SIGEMPTYMASK)
  210. #define sigpausex(sig) sigpause (0)
  211. #endif /* not BSD4_1 */
  212.  
  213. #ifdef BSD4_1
  214. #define SIGIO SIGTINT
  215. /* sigfree and sigholdx are in sysdep.c */
  216. #define sigblockx(sig) sighold (sig)
  217. #define sigunblockx(sig) sigrelse (sig)
  218. #define sigpausex(sig) sigpause (sig)
  219. #endif /* BSD4_1 */
  220.  
  221. /* We are unable to use interrupts if FIONREAD is not available,
  222.    so flush SIGIO so we won't try. */
  223. #ifndef FIONREAD
  224. #ifdef SIGIO
  225. #undef SIGIO
  226. #endif
  227. #endif
  228.  
  229. /* If we support X Windows, and won't get an interrupt when input
  230.    arrives from the server, poll periodically so we can detect C-g.  */
  231. #ifdef HAVE_X_WINDOWS
  232. #ifndef SIGIO
  233. #define POLL_FOR_INPUT
  234. #endif
  235. #endif
  236.  
  237. /* Function for init_keyboard to call with no args (if nonzero).  */
  238. void (*keyboard_init_hook) ();
  239.  
  240. static void read_avail_input ();
  241. static void get_input_pending ();
  242.  
  243. /* Non-zero tells input_available_signal to call read_socket_hook
  244.    even if FIONREAD returns zero.  */
  245. static int force_input;
  246.  
  247. static int echo_keystrokes;    /* > 0 if we are to echo keystrokes */
  248.  
  249. /* Nonzero means echo each character as typed.  */
  250. static int immediate_echo;
  251.  
  252. #define    min(a,b)    ((a)<(b)?(a):(b))
  253. #define    max(a,b)    ((a)>(b)?(a):(b))
  254.  
  255. static char echobuf[100];
  256. static char *echoptr;
  257.  
  258. /* Install the string STR as the beginning of the string of echoing,
  259.    so that it serves as a prompt for the next character.
  260.    Also start echoing.  */
  261.  
  262. echo_prompt (str)
  263.      char *str;
  264. {
  265.   int len = strlen (str);
  266.   if (len > sizeof echobuf - 4)
  267.     len = sizeof echobuf - 4;
  268.   bcopy (str, echobuf, len + 1);
  269.   echoptr = echobuf + len;
  270.  
  271.   echo ();
  272. }
  273.  
  274. /* Add the character C to the echo string,
  275.    if echoing is going on.  */
  276.  
  277. echo_char (c)
  278.      int c;
  279. {
  280.   extern char *push_key_description ();
  281.  
  282.   if (immediate_echo)
  283.     {
  284.       char *ptr = echoptr;
  285.  
  286.       if (ptr - echobuf > sizeof echobuf - 6)
  287.     return;
  288.  
  289.       if (echoptr != echobuf)
  290.     *ptr++ = ' ';
  291.  
  292.       ptr = push_key_description (c, ptr);
  293.       if (echoptr == echobuf && c == help_char)
  294.     {
  295.       strcpy (ptr, " (Type ? for further options)");
  296.       ptr += strlen (ptr);
  297.     }
  298.  
  299.       *ptr = 0;
  300.       echoptr = ptr;
  301.  
  302.       echo ();
  303.     }
  304. }
  305.  
  306. /* Temporarily add a dash to the end of the echo string,
  307.    so that it serves as a mini-prompt for the very next character.  */
  308.  
  309. echo_dash ()
  310. {
  311.   if (!immediate_echo && echoptr == echobuf)
  312.     return;
  313.  
  314.   /* Put a dash at the end of the buffer temporarily,
  315.      but make it go away when the next character is added.  */
  316.   echoptr[0] = '-';
  317.   echoptr[1] = 0;
  318.  
  319.   echo ();
  320. }
  321.  
  322. /* Display the current echo string, and begin echoing if not already
  323.    doing so.  */
  324.  
  325. echo ()
  326. {
  327.   if (!immediate_echo)
  328.     {
  329.       int i;
  330.       immediate_echo = 1;
  331.  
  332.       for (i = 0; i < this_command_key_count; i++)
  333.     echo_char (this_command_keys[i]);
  334.       echo_dash ();
  335.     }
  336.  
  337.   echoing = 1;
  338.   message1 (echobuf);
  339.   echoing = 0;
  340.  
  341.   if (waiting_for_input && !NULL (Vquit_flag))
  342.     quit_throw_to_read_command_char ();
  343. }
  344.  
  345. /* Turn off echoing, for the start of a new command.  */
  346.  
  347. cancel_echoing ()
  348. {
  349.   immediate_echo = 0;
  350.   echoptr = echobuf;
  351. }
  352.  
  353. /* When an auto-save happens, record the "time", and don't do again soon.  */
  354. record_auto_save ()
  355. {
  356.   last_auto_save = num_input_chars;
  357. }
  358.  
  359. Lisp_Object recursive_edit_unwind (), command_loop ();
  360.  
  361. DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
  362.   "Invoke the editor command loop recursively.\n\
  363. Do (throw 'exit nil) within the command loop to make this function return,\n\
  364. or (throw 'exit t) to make this function signal an error.\n\
  365. This function is called by the editor initialization\n\
  366. to begin editing.")
  367.   ()
  368. {
  369.   int count = specpdl_ptr - specpdl;
  370.  
  371.   command_loop_level++;
  372.   update_mode_lines = 1;
  373.  
  374.   record_unwind_protect (recursive_edit_unwind,
  375.              (current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)
  376.               ? Fcurrent_buffer ()
  377.               : Qnil));
  378.  
  379.   recursive_edit_1 ();
  380.  
  381.   unbind_to (count);
  382.   return Qnil;
  383. }
  384.  
  385. Lisp_Object
  386. recursive_edit_1 ()
  387. {
  388.   int count = specpdl_ptr - specpdl;
  389.   Lisp_Object val;
  390.  
  391.   if (command_loop_level > 0)
  392.     {
  393.       specbind (Qstandard_output, Qt);
  394.       specbind (Qstandard_input, Qt);
  395.     }
  396.  
  397.   val = command_loop ();
  398.   if (EQ (val, Qt))
  399.     Fsignal (Qquit, Qnil);
  400.  
  401.   unbind_to (count);
  402.   return Qnil;
  403. }
  404.  
  405. Lisp_Object
  406. recursive_edit_unwind (buffer)
  407.      Lisp_Object buffer;
  408. {
  409.   if (!NULL (buffer))
  410.     Fset_buffer (buffer);
  411.   command_loop_level--;
  412.   update_mode_lines = 1;
  413.   return Qnil;
  414. }
  415.  
  416. Lisp_Object
  417. cmd_error (data)
  418.      Lisp_Object data;
  419. {
  420.   Lisp_Object errmsg, tail, errname, file_error;
  421.   struct gcpro gcpro1;
  422.   int i;
  423.  
  424.   Vquit_flag = Qnil;
  425.   Vinhibit_quit = Qt;
  426.   Vstandard_output = Qt;
  427.   Vstandard_input = Qt;
  428.   Vexecuting_macro = Qnil;
  429.   echo_area_contents = 0;
  430.  
  431.   Fdiscard_input ();
  432.   bell ();
  433.  
  434.   errname = Fcar (data);
  435.  
  436.   if (EQ (errname, Qerror))
  437.     {
  438.       data = Fcdr (data);
  439.       if (!CONSP (data)) data = Qnil;
  440.       errmsg = Fcar (data);
  441.       file_error = Qnil;
  442.     }
  443.   else
  444.     {
  445.       errmsg = Fget (errname, Qerror_message);
  446.       file_error = Fmemq (Qfile_error,
  447.               Fget (errname, Qerror_conditions));
  448.     }
  449.  
  450.   /* Print an error message including the data items.
  451.      This is done by printing it into a scratch buffer
  452.      and then making a copy of the text in the buffer. */
  453.   
  454.   if (!CONSP (data)) data = Qnil;
  455.   tail = Fcdr (data);
  456.   GCPRO1 (tail);
  457.  
  458.   /* For file-error, make error message by concatenating
  459.      all the data items.  They are all strings.  */
  460.   if (!NULL (file_error))
  461.     errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
  462.  
  463.   if (XTYPE (errmsg) == Lisp_String)
  464.     Fprinc (errmsg, Qt);
  465.   else
  466.     write_string_1 ("peculiar error", -1, Qt);
  467.  
  468.   for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
  469.     {
  470.       write_string_1 (i ? ", " : ": ", 2, Qt);
  471.       if (!NULL (file_error))
  472.     Fprinc (Fcar (tail), Qt);
  473.       else
  474.     Fprin1 (Fcar (tail), Qt);
  475.     }
  476.   UNGCPRO;
  477.  
  478.   /* In -batch mode, force out the error message and newlines after it
  479.      and then die.  */
  480.   if (noninteractive)
  481.     {
  482.       message ("");
  483.       Fkill_emacs (make_number (-1));
  484.     }
  485.  
  486.   Vquit_flag = Qnil;
  487.  
  488.   Vinhibit_quit = Qnil;
  489.   return make_number (0);
  490. }
  491.  
  492. Lisp_Object command_loop_1 ();
  493. Lisp_Object command_loop_2 ();
  494. Lisp_Object cmd_error ();
  495. Lisp_Object top_level_1 ();
  496.  
  497. /* Entry to editor-command-loop.
  498.    This level has the catches for exiting/returning to editor command loop.
  499.    It returns nil to exit recursive edit, t to abort it.  */
  500.  
  501. Lisp_Object
  502. command_loop ()
  503. {
  504.   if (command_loop_level > 0 || minibuf_level > 0)
  505.     {
  506.       return internal_catch (Qexit, command_loop_2, Qnil);
  507.     }
  508.   else
  509.     while (1)
  510.       {
  511.     internal_catch (Qtop_level, top_level_1, Qnil);
  512.     internal_catch (Qtop_level, command_loop_2, Qnil);
  513.     /* End of file in -batch run causes exit here.  */
  514.     if (noninteractive)
  515.       Fkill_emacs (Qt);
  516.       }
  517. }
  518.  
  519. /* Here we catch errors in execution of commands within the
  520.    editing loop, and reenter the editing loop.
  521.    When there is an error, cmd_error runs and returns a non-nil
  522.    value to us.  A value of nil means that cmd_loop_1 itself
  523.    returned due to end of file (or end of kbd macro).  */
  524.  
  525. Lisp_Object
  526. command_loop_2 ()
  527. {
  528.   register Lisp_Object val;
  529.   do
  530.     val = internal_condition_case (command_loop_1, Qerror, cmd_error);
  531.   while (!NULL (val));
  532.   return Qnil;
  533. }
  534.  
  535. Lisp_Object
  536. top_level_2 ()
  537. {
  538.   return Feval (Vtop_level);
  539. }
  540.  
  541. Lisp_Object
  542. top_level_1 ()
  543. {
  544.   /* On entry to the outer level, run the startup file */
  545.   if (!NULL (Vtop_level))
  546.     internal_condition_case (top_level_2, Qerror, cmd_error);
  547.   else if (!NULL (Vpurify_flag))
  548.     message ("Bare impure Emacs (standard Lisp code not loaded)");
  549.   else
  550.     message ("Bare Emacs (standard Lisp code not loaded)");
  551.   return Qnil;
  552. }
  553.  
  554. DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
  555.   "Exit all recursive editing levels.")
  556.   ()
  557. {
  558.   Fthrow (Qtop_level, Qnil);
  559. }
  560.  
  561. DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
  562.   "Exit from the innermost recursive edit or minibuffer.")
  563.   ()
  564. {
  565.   if (command_loop_level > 0 || minibuf_level > 0)
  566.     Fthrow (Qexit, Qnil);
  567.   error ("No recursive edit is in progress");
  568. }
  569.  
  570. DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
  571.   "Abort the command that requested this recursive edit or minibuffer input.")
  572.   ()
  573. {
  574.   if (command_loop_level > 0 || minibuf_level > 0)
  575.     Fthrow (Qexit, Qt);
  576.   error ("No recursive edit is in progress");
  577. }
  578.  
  579. /* This is the actual command reading loop,
  580.  sans error-handling encapsulation */
  581.  
  582. Lisp_Object Fcommand_execute ();
  583.  
  584. Lisp_Object
  585. command_loop_1 ()
  586. {
  587.   Lisp_Object cmd;
  588.   int lose;
  589.   int nonundocount;
  590.   char keybuf[30];
  591.   int i;
  592.   int no_redisplay;
  593.   int no_direct;
  594.  
  595.   Vprefix_arg = Qnil;
  596.   waiting_for_input = 0;
  597.   cancel_echoing ();
  598.  
  599.   /* Don't clear out last_command at the beginning of a macro.  */
  600.   if (NULL (Vexecuting_macro)
  601.       || XTYPE (Vexecuting_macro) != Lisp_String)
  602.     last_command = Qt;
  603.   nonundocount = 0;
  604.   no_redisplay = 0;
  605.   this_command_key_count = 0;
  606.   
  607.   while (1)
  608.     {
  609.       /* Install chars successfully executed in kbd macro */
  610.       if (defining_kbd_macro && NULL (Vprefix_arg))
  611.     finalize_kbd_macro_chars ();
  612.  
  613.       /* Make sure current window's buffer is selected.  */
  614.  
  615.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  616.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  617.  
  618.       /* Display any malloc warning that just came out.
  619.      Use while because displaying one warning can cause another.  */
  620.       while (pending_malloc_warning)
  621.     display_malloc_warning ();
  622.  
  623.       no_direct = 0;
  624.  
  625.       /* If minibuffer on and echo area in use,
  626.      wait 2 sec and redraw minibufer.  */
  627.  
  628.       if (minibuf_level && echo_area_contents)
  629.     {
  630.       int count = specpdl_ptr - specpdl;
  631.       specbind (Qinhibit_quit, Qt);
  632.       Fsit_for (make_number (2), Qnil);
  633.       unbind_to (count);
  634.  
  635.       echo_area_contents = 0;
  636.       no_direct = 1;
  637.       if (!NULL (Vquit_flag))
  638.         {
  639.           Vquit_flag = Qnil;
  640.           unread_command_char = quit_char;
  641.         }
  642.     }
  643.  
  644.       i = 0;
  645. #if 0
  646.       /* If prev. command was directly displayed, we don't need
  647.      redisplay.  Try shortcut for reading single-char key sequence.  */
  648.       if (no_redisplay)
  649.     i = fast_read_one_key (keybuf);
  650. #endif /* 0 */
  651.       /* Shortcut not applicable or found a prefix key.
  652.      Take full precautions and read key sequence the hard way.  */
  653.       if (i == 0)
  654.     {
  655. #ifdef C_ALLOCA
  656.       alloca (0);        /* Cause a garbage collection now */
  657.                 /* Since we can free the most stuff here.  */
  658. #endif /* C_ALLOCA */
  659.  
  660.       /* Read next key sequence; i gets its length.  */
  661.  
  662.       i = read_key_sequence (keybuf, sizeof keybuf, 0,
  663.                  no_redisplay && buffer_shared <= 1);
  664.     }
  665.  
  666.       /* Now we have read a key sequence of length I,
  667.      or else I is 0 and we found end of file.  */
  668.  
  669.       if (i == 0)        /* End of file -- happens only in */
  670.     return Qnil;        /* a kbd macro, at the end */
  671.  
  672.       last_command_char = keybuf[i - 1];
  673.       
  674.       cmd = read_key_sequence_cmd;
  675.       if (!NULL (Vexecuting_macro))
  676.     {
  677.       if (!NULL (Vquit_flag))
  678.         {
  679.           Vexecuting_macro = Qt;
  680.           QUIT;        /* Make some noise. */
  681.                 /* Will return since macro now empty. */
  682.         }
  683.     }
  684.  
  685.       /* Do redisplay processing after this command except in special
  686.      cases identified below that set no_redisplay to 1.  */
  687.       no_redisplay = 0;
  688.  
  689.       /* Execute the command.  */
  690.  
  691.       if (NULL (cmd))
  692.     {
  693.       /* nil means key is undefined.  */
  694.       bell ();
  695.       defining_kbd_macro = 0;
  696.       update_mode_lines++;
  697.       Vprefix_arg = Qnil;
  698.     }
  699.       else
  700.     {
  701.       this_command = cmd;
  702.       if (NULL (Vprefix_arg) && ! no_direct)
  703.         {
  704.           if (EQ (cmd, Qforward_char) && point < ZV)
  705.         {
  706.           lose = FETCH_CHAR (point);
  707.           SET_PT (point + 1);
  708.           if (lose >= ' ' && lose < 0177
  709.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  710.               >= MODIFF)
  711.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  712.               == point)
  713.               && !windows_or_buffers_changed
  714.               && EQ (current_buffer->selective_display, Qnil)
  715.               && !detect_input_pending ()
  716.               && NULL (Vexecuting_macro))
  717.             no_redisplay = direct_output_forward_char (1);
  718.           goto directly_done;
  719.         }
  720.           else if (EQ (cmd, Qbackward_char) && point > BEGV)
  721.         {
  722.           SET_PT (point - 1);
  723.           lose = FETCH_CHAR (point);
  724.           if (lose >= ' ' && lose < 0177
  725.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  726.               >= MODIFF)
  727.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  728.               == point)
  729.               && !windows_or_buffers_changed
  730.               && EQ (current_buffer->selective_display, Qnil)
  731.               && !detect_input_pending ()
  732.               && NULL (Vexecuting_macro))
  733.             no_redisplay = direct_output_forward_char (-1);
  734.           goto directly_done;
  735.         }
  736.           else if (EQ (cmd, Qself_insert_command))
  737.         {
  738.           if (NULL (Vexecuting_macro) &&
  739.               !EQ (minibuf_window, selected_window))
  740.             {
  741.               if (!nonundocount || nonundocount >= 20)
  742.             {
  743.               Fundo_boundary ();
  744.               nonundocount = 0;
  745.             }
  746.               nonundocount++;
  747.             }
  748.           lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
  749.               < MODIFF)
  750.             || (XFASTINT (XWINDOW (selected_window)->last_point)
  751.               != point)
  752.             || MODIFF <= current_buffer->save_modified
  753.             || windows_or_buffers_changed
  754.             || !EQ (current_buffer->selective_display, Qnil)
  755.             || detect_input_pending ()
  756.             || !NULL (Vexecuting_macro);
  757.           if (self_insert_internal (last_command_char, 0))
  758.             {
  759.               lose = 1;
  760.               nonundocount = 0;
  761.             }
  762.           if (!lose
  763.               && (point == ZV || FETCH_CHAR (point) == '\n')
  764.               && last_command_char >= ' '
  765.               && last_command_char < 0177)
  766.             no_redisplay
  767.               = direct_output_for_insert (last_command_char);
  768.           goto directly_done;
  769.         }
  770.         }
  771.  
  772.       /* Here for a command that isn't executed directly */
  773.  
  774.       nonundocount = 0;
  775.       if (NULL (Vprefix_arg))
  776.         Fundo_boundary ();
  777.       Fcommand_execute (cmd, Qnil);
  778.     }
  779.       /* This label logically belongs inside the above group,
  780.      but moving it is said to avoid a compiler bug on SCO V.3.2v2.  */
  781.     directly_done: ;
  782.  
  783.       if (NULL (Vprefix_arg))
  784.     {
  785.       last_command = this_command;
  786.       this_command_key_count = 0;
  787.       cancel_echoing ();
  788.     }
  789.     }
  790. }
  791.  
  792. /* Input of single characters from keyboard */
  793.  
  794. Lisp_Object print_help ();
  795.  
  796. int echo_flag;
  797. int echo_now;
  798.  
  799. /* Alarm interrupt calls this and requests echoing at earliest safe time. */
  800. request_echo ()
  801. {
  802.   int old_errno = errno;
  803.  
  804.   /* Note: no need to reestablish handler on USG systems
  805.      because it is established, if approriate, each time an alarm is requested.  */
  806. #ifdef subprocesses
  807. #ifdef BSD4_1
  808.   extern int select_alarmed;
  809.   if (select_alarmed == 0)
  810.     {
  811.       select_alarmed = 1;
  812.       sigrelse (SIGALRM);
  813.       return;
  814.     }
  815. #endif
  816. #endif
  817.  
  818. #ifdef BSD4_1
  819.   sigisheld (SIGALRM);
  820. #endif
  821.  
  822.   if (echo_now)
  823.     echo ();
  824.   else
  825.     echo_flag = 1;
  826.  
  827. #ifdef BSD4_1
  828.   sigunhold (SIGALRM);
  829. #endif
  830.  
  831.   errno = old_errno;
  832. }
  833.  
  834. /* Nonzero means polling for input is temporarily suppresed.  */
  835. int poll_suppress_count;
  836.  
  837. /* Number of seconds between polling for input.  */
  838. int polling_period;
  839.  
  840. #ifdef POLL_FOR_INPUT
  841. int polling_for_input;
  842.  
  843. /* Handle an alarm once each second and read pending input
  844.    so as to handle a C-g if it comces in.  */
  845.  
  846. input_poll_signal ()
  847. {
  848.   int junk;
  849.  
  850.   if (!waiting_for_input)
  851.     read_avail_input (&junk);
  852.   signal (SIGALRM, input_poll_signal);
  853.   alarm (polling_period);
  854. }
  855.  
  856. #endif
  857.  
  858. /* Begin signals to poll for input, if they are appropriate.
  859.    This function is called unconditionally from various places.  */
  860.  
  861. start_polling ()
  862. {
  863. #ifdef POLL_FOR_INPUT
  864.   if (read_socket_hook)
  865.     {
  866.       poll_suppress_count--;
  867.       if (poll_suppress_count == 0)
  868.     {
  869.       signal (SIGALRM, input_poll_signal);
  870.       polling_for_input = 1;
  871.       alarm (polling_period);
  872.     }
  873.     }
  874. #endif
  875. }
  876.  
  877. /* Turn off polling.  */
  878.  
  879. stop_polling ()
  880. {
  881. #ifdef POLL_FOR_INPUT
  882.   if (read_socket_hook)
  883.     {
  884.       if (poll_suppress_count == 0)
  885.     {
  886.       polling_for_input = 0;
  887.       alarm (0);
  888.     }
  889.       poll_suppress_count++;
  890.     }
  891. #endif
  892. }
  893.  
  894. /* read a character from the keyboard; call the redisplay if needed */
  895. /* commandflag 0 means do not do auto-saving, but do do redisplay.
  896.    -1 means do not do redisplay, but do do autosaving.
  897.    1 means do both.  */
  898.  
  899. read_command_char (commandflag)
  900.      int commandflag;
  901. {
  902.   register int c;
  903.   int alarmtime;
  904.   int count;
  905.   Lisp_Object tem;
  906.   jmp_buf save_jump;
  907.   extern request_echo ();
  908.  
  909.   if (unread_command_char >= 0)
  910.     {
  911.       c = unread_command_char;
  912.       unread_command_char = -1;
  913.       if (this_command_key_count == 0)
  914.     goto reread_first;
  915.       goto reread;
  916.     }
  917.  
  918.   if (!NULL (Vexecuting_macro))
  919.     {
  920.       if (XTYPE (Vexecuting_macro) != Lisp_String
  921.       || XSTRING (Vexecuting_macro)->size <= executing_macro_index)
  922.     return -1;
  923.       QUIT;
  924.       c = XSTRING (Vexecuting_macro)->data[executing_macro_index++];
  925.       goto from_macro;
  926.     }
  927.  
  928.   /* Save outer setjmp data, in case called recursively.  */
  929.   bcopy (getcjmp, save_jump, sizeof getcjmp);
  930.  
  931.   stop_polling ();
  932.  
  933.   if (commandflag >= 0 && !detect_input_pending ())
  934.     redisplay ();
  935.  
  936.   if (commandflag != 0
  937.       && auto_save_interval > 0
  938.       && num_input_chars - last_auto_save > max (auto_save_interval, 20)
  939.       && !detect_input_pending ())
  940.     Fdo_auto_save (Qnil);
  941.  
  942.   if (_setjmp (getcjmp))
  943.     {
  944.       c = quit_char;
  945.       waiting_for_input = 0;
  946.       input_available_clear_word = 0;
  947.  
  948.       goto non_reread;
  949.     }
  950.  
  951.   /* Message turns off echoing unless more keystrokes turn it on again. */
  952.   if (echo_area_contents && *echo_area_contents && echo_area_contents != echobuf)
  953.     cancel_echoing ();
  954.   else
  955.     /* If already echoing, continue, and prompt.  */
  956.     echo_dash ();
  957.  
  958.   /* If in middle of key sequence and minibuffer not active,
  959.      start echoing if enough time elapses.  */
  960.   if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
  961.       && echo_keystrokes > 0
  962.       && (echo_area_contents == 0 || *echo_area_contents == 0))
  963.     {
  964.       /* Else start echoing if user waits more than `alarmtime' seconds. */
  965.       /* This interrupt either calls echo right away
  966.      or sets echo_flag, which causes echo to be called
  967.      by set_waiting_for_input's next invocation.  */
  968.       signal (SIGALRM, request_echo);
  969.       echo_flag = 0;
  970.       echo_now = 0;
  971.       alarmtime = echo_keystrokes;
  972.       alarm ((unsigned) alarmtime);
  973.     }
  974.  
  975.   c = kbd_buffer_read_command_char ();
  976.  
  977.   /* Terminate Emacs in batch mode if at eof.  */
  978.   if (noninteractive && c < 0)
  979.     Fkill_emacs (make_number (1));
  980.  
  981.  non_reread:
  982.  
  983.   bcopy (save_jump, getcjmp, sizeof getcjmp);
  984.  
  985.   /* Cancel alarm if it was set and has not already gone off. */
  986.   if (alarmtime > 0) alarm (0);
  987.  
  988.   echo_area_contents = 0;
  989.  
  990.   if (c < 0) return -1;
  991.  
  992.   c &= meta_key ? 0377 : 0177;
  993.  
  994.   if (XTYPE (Vkeyboard_translate_table) == Lisp_String
  995.       && XSTRING (Vkeyboard_translate_table)->size > c)
  996.     c = XSTRING (Vkeyboard_translate_table)->data[c];
  997.  
  998.   total_keys++;
  999.   recent_keys[recent_keys_index] = c;
  1000.   recent_keys_index = (recent_keys_index + 1) % sizeof recent_keys;
  1001.  
  1002.   if (dribble)
  1003.     {
  1004.       putc (c, dribble);
  1005.       fflush (dribble);
  1006.     }
  1007.  
  1008.   store_kbd_macro_char (c);
  1009.  
  1010.   start_polling ();
  1011.  
  1012.  from_macro:
  1013.  reread_first:  /* Rereading a char and it is the first in a command.  */
  1014.  
  1015.   echo_char (c);
  1016.  
  1017.   /* Record this character as part of the current key.  */
  1018.   if (this_command_key_count == this_command_keys_size)
  1019.     {
  1020.       this_command_keys_size *= 2;
  1021.       this_command_keys
  1022.     = (unsigned char *) xrealloc (this_command_keys,
  1023.                       this_command_keys_size);
  1024.     }
  1025.   this_command_keys[this_command_key_count++] = c;
  1026.  
  1027.   /* Rereading in the middle of a command.  */
  1028.  reread:
  1029.  
  1030.   last_input_char = c;
  1031.  
  1032.   num_input_chars++;
  1033.  
  1034.   /* Process the help character specially if enabled */
  1035.   if (c == help_char && !NULL (Vhelp_form))
  1036.     {
  1037.       count = specpdl_ptr - specpdl;
  1038.  
  1039.       record_unwind_protect (Fset_window_configuration,
  1040.                  Fcurrent_window_configuration ());
  1041.  
  1042.       tem = Feval (Vhelp_form);
  1043.       if (XTYPE (tem) == Lisp_String)
  1044.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem);
  1045.  
  1046.       cancel_echoing ();
  1047.       c = read_command_char (0);
  1048.       /* Remove the help from the screen */
  1049.       unbind_to (count);
  1050.       redisplay ();
  1051.       if (c == 040)
  1052.     {
  1053.       cancel_echoing ();
  1054.       c = read_command_char (0);
  1055.     }
  1056.     }
  1057.  
  1058.   return c;
  1059. }
  1060.  
  1061. Lisp_Object
  1062. print_help (object)
  1063.      Lisp_Object object;
  1064. {
  1065.   Fprinc (object, Qnil);
  1066.   return Qnil;
  1067. }
  1068.  
  1069. /* Low level keyboard input.
  1070.  Read characters into kbd_buffer
  1071.  from which they are obtained by kbd_buffer_read_command_char.  */
  1072.  
  1073. /* Set this for debugging, to have a way to get out */
  1074. int stop_character;
  1075.  
  1076. /* Store a character obtained at interrupt level into kbd_buffer, fifo */
  1077. kbd_buffer_store_char (c)
  1078.      register int c;
  1079. {
  1080.   c &= 0377;
  1081.  
  1082.   if (c == quit_char
  1083.       || ((c == (0200 | quit_char)) && !meta_key))
  1084.     {
  1085.       interrupt_signal ();
  1086.       return;
  1087.     }
  1088.  
  1089.   if (c && c == stop_character)
  1090.     {
  1091.       sys_suspend ();
  1092.       return;
  1093.     }
  1094.  
  1095.   if (kbd_ptr != kbd_buffer)
  1096.     {
  1097.       bcopy (kbd_ptr, kbd_buffer, kbd_count);
  1098.       kbd_ptr = kbd_buffer;
  1099.     }
  1100.  
  1101.   if (kbd_count < sizeof kbd_buffer)
  1102.     {
  1103.       kbd_buffer[kbd_count++] = c;
  1104.     }
  1105. }
  1106.  
  1107. kbd_buffer_read_command_char ()
  1108. {
  1109.   register int c;
  1110.  
  1111.   if (noninteractive)
  1112.     {
  1113.       c = getchar ();
  1114.       return c;
  1115.     }
  1116.  
  1117.   /* Either ordinary input buffer or C-g buffered means we can return.  */
  1118.   while (!kbd_count)
  1119.     {
  1120.       if (!NULL (Vquit_flag))
  1121.     quit_throw_to_read_command_char ();
  1122.  
  1123.       /* One way or another, wait until input is available; then, if
  1124.      interrupt handlers have not read it, read it now.  */
  1125.  
  1126. #ifdef VMS
  1127.       wait_for_kbd_input ();
  1128. #else
  1129. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1130. #ifdef SIGIO
  1131.       gobble_input ();
  1132. #endif /* SIGIO */
  1133.       if (!kbd_count)
  1134.     {
  1135. #ifdef subprocesses
  1136.       wait_reading_process_input (0, -1, 1);
  1137. #else
  1138. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1139. #ifdef SIGIO
  1140.       if (interrupt_input)
  1141.         {
  1142.           sigblockx (SIGIO);
  1143.           set_waiting_for_input (0);
  1144.           while (!kbd_count)
  1145.         sigpausex (SIGIO);
  1146.           clear_waiting_for_input ();
  1147.           sigunblockx (SIGIO);
  1148.         }
  1149. #else
  1150.       interrupt_input = 0;
  1151. #endif /* not SIGIO */
  1152. #endif /* subprocesses */
  1153.  
  1154.       if (!interrupt_input && !kbd_count)
  1155.         {
  1156.           read_avail_input (0);
  1157.         }
  1158.     }
  1159. #endif /* not VMS */
  1160.     }
  1161.  
  1162.   input_pending = --kbd_count > 0;
  1163.   c = *kbd_ptr;            /* *kbd_ptr++ would have a timing error. */
  1164.   kbd_ptr++;            /* See kbd_buffer_store_char. */
  1165.   return (c & (meta_key ? 0377 : 0177)); /* Clean up if sign was extended. */
  1166. }
  1167.  
  1168. /* Force an attempt to read input regardless of what FIONREAD says.  */
  1169.  
  1170. force_input_read ()
  1171. {
  1172.   force_input = 1;
  1173.   detect_input_pending ();
  1174.   force_input = 0;
  1175. }
  1176.  
  1177. /* Store into *addr the number of terminal input chars available.
  1178.    Equivalent to ioctl (0, FIONREAD, addr) but works
  1179.    even if FIONREAD does not exist.  */
  1180.  
  1181. static void
  1182. get_input_pending (addr)
  1183.      int *addr;
  1184. {
  1185. #ifdef VMS
  1186.   /* On VMS, we always have something in the buffer
  1187.      if any input is available.  */
  1188.   /*** It might be simpler to make interrupt_input 1 on VMS ***/
  1189.   *addr = kbd_count | !NULL (Vquit_flag);
  1190. #else
  1191.   /* First of all, have we already counted some input?  */
  1192.   *addr = kbd_count | !NULL (Vquit_flag);
  1193.   /* If input is being read as it arrives, and we have none, there is none.  */
  1194.   if (*addr > 0 || (interrupt_input && ! interrupts_deferred && ! force_input))
  1195.     return;
  1196. #ifdef FIONREAD
  1197.   if (! force_input)
  1198.     {
  1199.       /* If we can count the input without reading it, do so.  */
  1200.       if (ioctl (0, FIONREAD, addr) < 0)
  1201.     *addr = 0;
  1202.       if (*addr == 0 || read_socket_hook == 0)
  1203.     return;
  1204.       /* If the input consists of window-events, not all of them
  1205.      are necessarily kbd chars.  So process all the input
  1206.      and see how many kbd chars we got.  */
  1207.     }
  1208. #endif
  1209. #ifdef SIGIO
  1210.   {
  1211.     /* It seems there is a timing error such that a SIGIO can be handled here
  1212.        and cause kbd_count to become nonzero even though raising of SIGIO
  1213.        has already been turned off.  */
  1214.     SIGMASKTYPE mask = sigblock (sigmask (SIGIO));
  1215.     if (kbd_count == 0)
  1216.       read_avail_input (*addr);
  1217.     sigsetmask (mask);
  1218.   }
  1219. #else
  1220.   /* If we can't count the input, read it (if any) and see what we got.  */
  1221.   read_avail_input (*addr);
  1222. #endif
  1223.   *addr = kbd_count | !NULL (Vquit_flag);
  1224. #endif
  1225. }
  1226.  
  1227. /* Read pending any input out of the system and into Emacs.  */
  1228.  
  1229. /* This function is temporary in Emacs 18.  It is used only
  1230.    with X windows.  X windows always turns on interrupt input
  1231.    if possible, so this function has nothing to do except
  1232.    on systems that don't have SIGIO.  And they also don't have FIONREAD.  */
  1233. void
  1234. consume_available_input ()
  1235. {
  1236. #ifdef SIGIO
  1237.   if (!interrupt_input || interrupts_deferred)
  1238. #endif
  1239.     read_avail_input (0);
  1240. }
  1241.  
  1242. /* Read any terminal input already buffered up by the system
  1243.    into the kbd_buffer, assuming the buffer is currently empty.
  1244.    Never waits.
  1245.  
  1246.    If NREAD is nonzero, assume it contains # chars of raw data waiting.
  1247.    If it is zero, we determine that datum.
  1248.  
  1249.    Input gets into the kbd_buffer either through this function
  1250.    (at main program level) or at interrupt level if input
  1251.    is interrupt-driven.  */
  1252.  
  1253. static void
  1254. read_avail_input (nread)
  1255.      int nread;
  1256. {
  1257.   /* This function is not used on VMS.  */
  1258. #ifndef VMS
  1259.   char buf[256 * BUFFER_SIZE_FACTOR];
  1260.   register int i;
  1261.  
  1262. #ifdef FIONREAD
  1263.   if (! force_input)
  1264.     {
  1265.       if (nread == 0)
  1266.     get_input_pending (&nread);
  1267.       if (nread == 0)
  1268.     return;
  1269.     }
  1270.   if (nread > sizeof buf)
  1271.     nread = sizeof buf;
  1272.  
  1273.   /* Read what is waiting.  */
  1274.   if (read_socket_hook)
  1275.     nread = (*read_socket_hook) (0, buf, nread);
  1276.   else
  1277.     nread = read (0, buf, nread);
  1278.  
  1279. #else /* no FIONREAD */
  1280. #ifdef USG
  1281. #ifdef SYSV_STREAMS
  1282.   /* When talking to Xwindows using streams, something gets screwed up
  1283.      if Emacs alters this flag in the descriptor.  */
  1284.   if (!read_socket_hook)
  1285. #endif
  1286.     fcntl (fileno (stdin), F_SETFL, O_NDELAY);
  1287.   if (read_socket_hook)
  1288.     {
  1289.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1290.     }
  1291.   else
  1292.     {
  1293.       nread = read (fileno (stdin), buf, sizeof buf);
  1294.     }
  1295. #ifdef AIX
  1296.   /* The kernel sometimes fails to deliver SIGHUP for ptys.
  1297.      This looks incorrect, but it isn't, because _BSD causes
  1298.      O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
  1299.      and that causes a value other than 0 when there is no input.  */
  1300.   if (nread == 0)
  1301.     kill (SIGHUP, 0);
  1302. #endif
  1303. #ifdef EBADSLT
  1304.   if (nread == -1 && (errno == EAGAIN || errno == EBADSLT))
  1305. #else
  1306.   if (nread == -1 && errno == EAGAIN)
  1307. #endif
  1308.     nread = 0;
  1309. #ifdef SYSV_STREAMS
  1310.   if (!read_socket_hook)
  1311. #endif
  1312.     fcntl (fileno (stdin), F_SETFL, 0);
  1313. #else /* not USG */
  1314. #ifdef    AMIGA    /* This is where the input work finally gets done */
  1315.   /* Note, The nread != 0 case isn't handled as it doesn't arise on the Amiga.
  1316.      (Look carefully at calls to read_avail_input) */
  1317.   nread = read(0, buf, sizeof buf);
  1318. #else  /* not AMIGA */
  1319.   you lose
  1320. #endif /* not AMIGA */
  1321. #endif /* not USG */
  1322. #endif /* no FIONREAD */
  1323.  
  1324.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  1325.   if (kbd_count == 0)
  1326.     kbd_ptr = kbd_buffer;
  1327.  
  1328.   for (i = 0; i < nread; i++)
  1329.     {
  1330.       kbd_buffer_store_char (buf[i]);
  1331.       /* Don't look at input that follows a C-g too closely.
  1332.      This reduces lossage due to autorepeat on C-g.  */
  1333.       if (buf[i] == quit_char)
  1334.     break;
  1335.     }
  1336. #endif /* not VMS */
  1337. }
  1338.  
  1339. #ifdef SIGIO   /* for entire page */
  1340. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  1341.  
  1342. /* If using interrupt input and some input chars snuck into the
  1343.    buffer before we enabled interrupts, fake an interrupt for them.  */
  1344.  
  1345. gobble_input ()
  1346. {
  1347.   int nread;
  1348.   if (interrupt_input)
  1349.     {
  1350.       if (ioctl (0, FIONREAD, &nread) < 0)
  1351.     nread = 0;
  1352.       if (nread)
  1353.     {
  1354.       sigholdx (SIGIO);
  1355.       input_available_signal (SIGIO);
  1356.       sigfree ();
  1357.     }
  1358.     }
  1359. }
  1360.  
  1361. input_available_signal (signo)
  1362.      int signo;
  1363. {
  1364.   unsigned char buf[256 * BUFFER_SIZE_FACTOR];
  1365.   int nread;
  1366.   register int i;
  1367.   /* Must preserve main program's value of errno.  */
  1368.   int old_errno = errno;
  1369. #ifdef BSD4_1
  1370.   extern int select_alarmed;
  1371. #endif
  1372.  
  1373. #ifdef USG
  1374.   /* USG systems forget handlers when they are used;
  1375.      must reestablish each time */
  1376.   signal (signo, input_available_signal);
  1377. #endif /* USG */
  1378.  
  1379. #ifdef BSD4_1
  1380.   sigisheld (SIGIO);
  1381. #endif
  1382.  
  1383.   if (input_available_clear_word)
  1384.     *input_available_clear_word = 0;
  1385.  
  1386.   while (1)
  1387.     {
  1388.       if (ioctl (0, FIONREAD, &nread) < 0)
  1389.     /* Formerly simply exited the loop, but that sometimes led to
  1390.        a failure of Emacs to terminate.
  1391.        SIGHUP seems appropriate if we can't reach the terminal.  */
  1392.     kill (getpid (), SIGHUP);
  1393.       if (nread <= 0)
  1394.     break;
  1395. #ifdef BSD4_1
  1396.       select_alarmed = 1;  /* Force the select emulator back to life */
  1397. #endif
  1398.       if (read_socket_hook)
  1399.     {
  1400.       nread = (*read_socket_hook) (0, buf, sizeof buf);
  1401.       if (!nread)
  1402.         continue;
  1403.     }
  1404.       else
  1405.     {
  1406.       if (nread > sizeof buf)
  1407.         nread = sizeof buf;
  1408.       nread = read (0, buf, nread);
  1409.     }
  1410.  
  1411.       for (i = 0; i < nread; i++)
  1412.     {
  1413.       kbd_buffer_store_char (buf[i]);
  1414.       /* Don't look at input that follows a C-g too closely.
  1415.          This reduces lossage due to autorepeat on C-g.  */
  1416.       if (buf[i] == quit_char)
  1417.         break;
  1418.     }
  1419.     }
  1420. #ifdef BSD4_1
  1421.   sigfree ();
  1422. #endif
  1423.   errno = old_errno;
  1424. }
  1425. #endif /* SIGIO */
  1426.  
  1427. #if 0
  1428. /* This is turned off because it didn't produce much speedup.  */
  1429.  
  1430. /* Read a single-char key sequence.  Do not redisplay.
  1431.    Return 1 if successful, or 0 if what follows is not
  1432.    a single-char key.  (In that case, a char has been unread.)
  1433.    This is used instead of read_key_sequence as an optimization
  1434.    just after a direct-updating command is done, since at such
  1435.    times we know that no redisplay is required.  */
  1436.  
  1437. int
  1438. fast_read_one_key (keybuf)
  1439.      char *keybuf;
  1440. {
  1441.   register Lisp_Object map;
  1442.   register int c;
  1443.   register Lisp_Object tem;
  1444.  
  1445.   keys_prompt = 0;
  1446.   /* Read a character, and do not redisplay.  */
  1447.   c = read_command_char (-1);
  1448.   Vquit_flag = Qnil;
  1449.  
  1450.   /* Assume until further notice that we are unlucky
  1451.      and will return zero, so this char will be
  1452.      reread by read_key_sequence.  */
  1453.  
  1454.   unread_command_char = c;
  1455.  
  1456.   if (c < 0 || c >= 0200)
  1457.     return 0;
  1458.  
  1459.   map = current_buffer->keymap;
  1460.   if (!EQ (map, Qnil))
  1461.     {
  1462.       tem = get_keyelt (access_keymap (map, c));
  1463.       if (!EQ (tem, Qnil))
  1464.     return 0;
  1465.     }
  1466.  
  1467.   XSET (map, Lisp_Vector, global_map);
  1468.   tem = !NULL (map)
  1469.     ? get_keyelt (access_keymap (map, c))
  1470.       : Qnil;
  1471.  
  1472.   read_key_sequence_cmd = tem;
  1473.  
  1474.   /* trace symbols to their function definitions */
  1475.  
  1476.   while (XTYPE (tem) == Lisp_Symbol && !NULL (tem)
  1477.      && !EQ (tem, Qunbound))
  1478.     tem = XSYMBOL (tem)->function;
  1479.  
  1480.   /* Is the definition a prefix character?  */
  1481.  
  1482.   if (XTYPE (tem) == Lisp_Vector ||
  1483.       (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap)))
  1484.     return 0;
  1485.  
  1486.   unread_command_char = -1;
  1487.   keybuf[0] = c;
  1488.   return 1;
  1489. }
  1490.  
  1491. #endif /* 0 */
  1492.  
  1493. /* Read a sequence of keys that ends with a non prefix character,
  1494.  and store them in KEYBUF, a buffer of size BUFSIZE.
  1495.  Prompt with PROMPT.  Echo starting immediately unless `prompt' is 0.
  1496.  Return the length of the key sequence stored.
  1497.  NODISPLAY nonzero means don't do redisplay before the first character
  1498.  (just for speedup).  */
  1499.  
  1500. int
  1501. read_key_sequence (keybuf, bufsize, prompt, nodisplay)
  1502.      char *keybuf;
  1503.      int bufsize;
  1504.      unsigned char *prompt;
  1505.      int nodisplay;
  1506. {
  1507.   register int i;
  1508.   Lisp_Object nextlocal, nextglobal;
  1509.   register int c, nextc;
  1510.   Lisp_Object local, global;
  1511.  
  1512.   if (FROM_KBD)
  1513.     {
  1514.       if (prompt)
  1515.     echo_prompt (prompt);
  1516.       else if (cursor_in_echo_area)
  1517.     echo_dash ();
  1518.     }
  1519.  
  1520.   nextc = read_command_char (nodisplay ? -1 : !prompt);
  1521.   nextlocal = current_buffer->keymap;
  1522.   XSET (nextglobal, Lisp_Vector, global_map);
  1523.  
  1524.   i = 0;
  1525.   while (!NULL (nextlocal) || !NULL (nextglobal))
  1526.     {
  1527.       if (i == bufsize)
  1528.     error ("key sequence too long");
  1529.  
  1530.       if (nextc >= 0)
  1531.     {
  1532.       c = nextc;
  1533.       nextc = -1;
  1534.     }
  1535.       else
  1536.     c = read_command_char (!prompt);
  1537.       Vquit_flag = Qnil;
  1538.       nodisplay = 0;
  1539.  
  1540.       if (c < 0)
  1541.     return 0;
  1542.       if (c >= 0200)
  1543.     {
  1544.       nextc = c & 0177;
  1545.       c = meta_prefix_char;
  1546.     }
  1547.  
  1548.       keybuf[i] = c;
  1549.  
  1550.       global = !NULL (nextglobal)
  1551.     ? get_keyelt (access_keymap (nextglobal, c))
  1552.       : Qnil;
  1553.  
  1554.       local = !NULL (nextlocal)
  1555.     ? get_keyelt (access_keymap (nextlocal, c))
  1556.       : Qnil;
  1557.  
  1558.       /* If C is not defined in either keymap
  1559.      and it is an uppercase letter, try corresponding lowercase.  */
  1560.  
  1561.       if (NULL (global) && NULL (local) && UPPERCASEP (c))
  1562.     {
  1563.       global = !NULL (nextglobal)
  1564.         ? get_keyelt (access_keymap (nextglobal, DOWNCASE (c)))
  1565.           : Qnil;
  1566.  
  1567.       local = !NULL (nextlocal)
  1568.         ? get_keyelt (access_keymap (nextlocal, DOWNCASE (c)))
  1569.           : Qnil;
  1570.  
  1571.       /* If that has worked better that the original char,
  1572.          downcase it permanently.  */
  1573.  
  1574.       if (!NULL (global) || !NULL (local))
  1575.         {
  1576.           keybuf[i] = c = DOWNCASE (c);
  1577.         }
  1578.     }
  1579.  
  1580.       i++;
  1581.  
  1582.       nextlocal = Qnil;
  1583.       nextglobal = Qnil;
  1584.  
  1585.       read_key_sequence_cmd = !NULL (local) ? local : global;
  1586.  
  1587.       /* trace symbols to their function definitions */
  1588.  
  1589.       while (XTYPE (global) == Lisp_Symbol && !NULL (global)
  1590.          && !EQ (global, Qunbound))
  1591.     global = XSYMBOL (global)->function;
  1592.       while (XTYPE (local) == Lisp_Symbol && !NULL (local)
  1593.          && !EQ (local, Qunbound))
  1594.     local = XSYMBOL (local)->function;
  1595.  
  1596.       /* Are the definitions prefix characters? */
  1597.  
  1598.       if (XTYPE (local) == Lisp_Vector ||
  1599.       (CONSP (local) && EQ (XCONS (local)->car, Qkeymap))
  1600.       ||
  1601.       /* If nextc is set, we are processing a prefix char
  1602.          that represents a meta-bit.
  1603.          Let a global prefix definition override a local non-prefix.
  1604.          This is for minibuffers that redefine Escape for completion.
  1605.          A real Escape gets completion, but Meta bits get ESC-prefix.  */
  1606.       ((NULL (local) || nextc >= 0)
  1607.        && (XTYPE (global) == Lisp_Vector ||
  1608.            (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))))
  1609.     {
  1610.       if (XTYPE (local) == Lisp_Vector ||
  1611.           (CONSP (local) && EQ (XCONS (local)->car, Qkeymap)))
  1612.         nextlocal = local;
  1613.       else
  1614.         nextlocal = Qnil;
  1615.  
  1616.       if (XTYPE (global) == Lisp_Vector ||
  1617.           (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))
  1618.         nextglobal = global;
  1619.       else
  1620.         nextglobal = Qnil;
  1621.     }
  1622.     }
  1623.  
  1624.   return i;
  1625. }
  1626.  
  1627. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 1, 0,
  1628.   "Read a sequence of keystrokes and return as a string.\n\
  1629. The sequence is sufficient to specify a non-prefix command\n\
  1630. starting from the current local and global keymaps.\n\
  1631. A C-g typed while in this function is treated like\n\
  1632. any other character, and quit-flag is not set.\n\
  1633. One arg, PROMPT, a prompt string or  nil, meaning do not prompt specially.")
  1634.   (prompt)
  1635.      Lisp_Object prompt;
  1636. {
  1637.   char keybuf[30];
  1638.   register int i;
  1639.  
  1640.   if (!NULL (prompt))
  1641.     CHECK_STRING (prompt, 0);
  1642.   QUIT;
  1643.  
  1644.   this_command_key_count = 0;
  1645.   i = read_key_sequence (keybuf, sizeof keybuf,
  1646.              (NULL (prompt)) ? 0 : XSTRING (prompt)->data,
  1647.              0);
  1648.   return make_string (keybuf, i);
  1649. }
  1650.  
  1651. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
  1652.  "Execute CMD as an editor command.\n\
  1653. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  1654. Optional second arg RECORD-FLAG non-nil\n\
  1655. means unconditionally put this command in the command-history.\n\
  1656. Otherwise, this is done only if an arg is read using the minibuffer.")
  1657.      (cmd, record)
  1658.      Lisp_Object cmd, record;
  1659. {
  1660.   register Lisp_Object final;
  1661.   register Lisp_Object tem;
  1662.   Lisp_Object prefixarg;
  1663.   struct backtrace backtrace;
  1664.   extern int debug_on_next_call;
  1665.  
  1666.   prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
  1667.   Vcurrent_prefix_arg = prefixarg;
  1668.   debug_on_next_call = 0;
  1669.  
  1670.   if (XTYPE (cmd) == Lisp_Symbol)
  1671.     {
  1672.       tem = Fget (cmd, Qdisabled);
  1673.       if (!NULL (tem))
  1674.     return call0 (Vdisabled_command_hook);
  1675.     }
  1676.  
  1677.   while (1)
  1678.     {
  1679.       final = cmd;
  1680.       while (XTYPE (final) == Lisp_Symbol)
  1681.     {
  1682.       if (EQ (Qunbound, XSYMBOL (final)->function))
  1683.         Fsymbol_function (final);    /* Get an error! */
  1684.       final = XSYMBOL (final)->function;
  1685.     }
  1686.  
  1687.       if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
  1688.     do_autoload (final, cmd);
  1689.       else
  1690.     break;
  1691.     }
  1692.  
  1693.   if (CONSP (final) || XTYPE (final) == Lisp_Subr)
  1694.     {
  1695.       backtrace.next = backtrace_list;
  1696.       backtrace_list = &backtrace;
  1697.       backtrace.function = &Qcall_interactively;
  1698.       backtrace.args = &cmd;
  1699.       backtrace.nargs = 1;
  1700.       backtrace.evalargs = 0;
  1701.  
  1702.       tem = Fcall_interactively (cmd, record);
  1703.  
  1704.       backtrace_list = backtrace.next;
  1705.       return tem;
  1706.     }
  1707.   if (XTYPE (final) == Lisp_String)
  1708.     {
  1709.       return Fexecute_kbd_macro (final, prefixarg);
  1710.     }
  1711.   return Qnil;
  1712. }
  1713.  
  1714. DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
  1715.   1, 1, "P",
  1716.   "Read function name, then read its arguments and call it.")
  1717.   (prefixarg)
  1718.      Lisp_Object prefixarg;
  1719. {
  1720.   Lisp_Object function;
  1721.   char buf[40];
  1722.   Lisp_Object saved_keys;
  1723.   struct gcpro gcpro1;
  1724.  
  1725.   saved_keys = Fthis_command_keys ();
  1726.   GCPRO1 (saved_keys);
  1727.  
  1728.   buf[0] = 0;
  1729.  
  1730.   if (EQ (prefixarg, Qminus))
  1731.     strcpy (buf, "- ");
  1732.   else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
  1733.     strcpy (buf, "C-u ");
  1734.   else if (CONSP (prefixarg) && XTYPE (XCONS (prefixarg)->car) == Lisp_Int)
  1735.     sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
  1736.   else if (XTYPE (prefixarg) == Lisp_Int)
  1737.     sprintf (buf, "%d ", XINT (prefixarg));
  1738.  
  1739.   /* This isn't strictly correct if execute-extended-command
  1740.      is bound to anything else */
  1741.   strcat (buf, "M-x ");
  1742.  
  1743.   function = Fcompleting_read (build_string (buf), Vobarray, Qcommandp, Qt, Qnil);
  1744.  
  1745.   saved_keys = concat2 (saved_keys, function);
  1746.   if (this_command_keys_size < XSTRING (function)->size)
  1747.     {
  1748.       this_command_keys_size += XSTRING (function)->size;
  1749.       this_command_keys = (unsigned char *) xrealloc (this_command_keys,
  1750.                               this_command_keys_size);
  1751.     }
  1752.   bcopy (XSTRING (function)->data, this_command_keys,
  1753.      XSTRING (function)->size + 1);
  1754.   this_command_key_count = XSTRING (saved_keys)->size;
  1755.  
  1756.   UNGCPRO;
  1757.  
  1758.   function = Fintern (function, Vobarray);
  1759.   Vprefix_arg = prefixarg;
  1760.   this_command = function;
  1761.  
  1762.   return Fcommand_execute (function, Qt);
  1763. }
  1764.  
  1765. detect_input_pending ()
  1766. {
  1767.   if (!input_pending)
  1768.     get_input_pending (&input_pending);
  1769.  
  1770.   return input_pending;
  1771. }
  1772.  
  1773. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  1774.   "T if command input is currently available with no waiting.\n\
  1775. Actually, the value is NIL only if we can be sure that no input is available.")
  1776.   ()
  1777. {
  1778.   if (unread_command_char >= 0) return Qt;
  1779.  
  1780.   return detect_input_pending () ? Qt : Qnil;
  1781. }
  1782.  
  1783. DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
  1784.   "Return string of last 100 chars read from terminal.")
  1785.   ()
  1786. {
  1787.   Lisp_Object val;
  1788.   if (total_keys < sizeof recent_keys)
  1789.     return make_string (recent_keys, total_keys);
  1790.  
  1791.   val = make_string (recent_keys, sizeof recent_keys);
  1792.   bcopy (recent_keys + recent_keys_index,
  1793.      XSTRING (val)->data,
  1794.      sizeof recent_keys - recent_keys_index);
  1795.   bcopy (recent_keys,
  1796.      XSTRING (val)->data + sizeof recent_keys - recent_keys_index,
  1797.      recent_keys_index);
  1798.   return val;
  1799. }
  1800.  
  1801. DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
  1802.   "Return string of the keystrokes that invoked this command.")
  1803.   ()
  1804. {
  1805.   return make_string (this_command_keys, this_command_key_count);
  1806. }
  1807.  
  1808. DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
  1809.   "Return the current depth in recursive edits.")
  1810.   ()
  1811. {
  1812.   Lisp_Object temp;
  1813.   XFASTINT (temp) = command_loop_level + minibuf_level;
  1814.   return temp;
  1815. }
  1816.  
  1817. DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
  1818.   "FOpen dribble file: ",
  1819.   "Start writing all keyboard characters to FILE.\n\
  1820. Use nil as an argument to close the dribble file.")
  1821.   (file)
  1822.      Lisp_Object file;
  1823. {
  1824.   if (dribble != 0)
  1825.     fclose (dribble);
  1826.   dribble = 0;
  1827.   if (!NULL (file))
  1828.     {
  1829.       file = Fexpand_file_name (file, Qnil);
  1830.       dribble = fopen (XSTRING (file)->data, "w");
  1831.     }
  1832.   return Qnil;
  1833. }
  1834.  
  1835. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  1836.   "Discard the contents of the terminal input buffer.\n\
  1837. Also flush any kbd macro definition in progress.")
  1838.   ()
  1839. {
  1840.   defining_kbd_macro = 0;
  1841.   update_mode_lines++;
  1842.  
  1843.   unread_command_char = -1;
  1844.   discard_tty_input ();
  1845.  
  1846.   kbd_count = 0;
  1847.   input_pending = 0;
  1848.  
  1849.   return Qnil;
  1850. }
  1851.  
  1852. DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
  1853.   "Stop Emacs and return to superior process.  You can resume.\n\
  1854. If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
  1855. to be read as terminal input by Emacs's superior shell.\n\
  1856. Before suspending, if `suspend-hook' is bound and value is non-nil\n\
  1857. call the value as a function of no args.  Don't suspend if it returns non-nil.\n\
  1858. Otherwise, suspend normally and after resumption call\n\
  1859. `suspend-resume-hook' if that is bound and non-nil.")
  1860.   (stuffstring)
  1861.      Lisp_Object stuffstring;
  1862. {
  1863.   register Lisp_Object tem;
  1864.   int count = specpdl_ptr - specpdl;
  1865.   int old_height, old_width;
  1866.   int width, height;
  1867.   struct gcpro gcpro1;
  1868.   extern init_sys_modes ();
  1869.  
  1870.   if (!NULL (stuffstring))
  1871.     CHECK_STRING (stuffstring, 0);
  1872.   GCPRO1 (stuffstring);
  1873.  
  1874.   /* Call value of suspend-hook
  1875.      if it is bound and value is non-nil.  */
  1876.   tem = intern ("suspend-hook");
  1877.   tem = XSYMBOL (tem)->value;
  1878.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1879.     {
  1880.       tem = call0 (tem);
  1881.       if (!EQ (tem, Qnil)) return Qnil;
  1882.     }
  1883.  
  1884.   get_screen_size (&old_width, &old_height);
  1885.   reset_sys_modes ();
  1886.   /* sys_suspend can get an error if it tries to fork a subshell
  1887.      and the system resources aren't available for that.  */
  1888.   record_unwind_protect (init_sys_modes, 0);
  1889.   stuff_buffered_input (stuffstring);
  1890.   sys_suspend ();
  1891.   unbind_to (count);
  1892.  
  1893.   /* Check if terminal/window size has changed.
  1894.      Note that this is not useful when we are running directly
  1895.      with a window system; but suspend should be disabled in that case.  */
  1896.   get_screen_size (&width, &height);
  1897.   if (width != old_width || height != old_height)
  1898.     change_screen_size (height, width, 0, 0, 0);
  1899.  
  1900.   /* Call value of suspend-resume-hook
  1901.      if it is bound and value is non-nil.  */
  1902.   tem = intern ("suspend-resume-hook");
  1903.   tem = XSYMBOL (tem)->value;
  1904.   if (! EQ (tem, Qunbound) && ! EQ (tem, Qnil))
  1905.     call0 (tem);
  1906.   UNGCPRO;
  1907.   return Qnil;
  1908. }
  1909.  
  1910. /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
  1911.    Then in any case stuff anthing Emacs has read ahead and not used.  */
  1912.  
  1913. stuff_buffered_input (stuffstring)
  1914.      Lisp_Object stuffstring;
  1915. {
  1916.   register unsigned char *p;
  1917.  
  1918. /* stuff_char works only in BSD, versions 4.2 and up.  */
  1919. #ifdef BSD
  1920. #ifndef BSD4_1
  1921.   if (XTYPE (stuffstring) == Lisp_String)
  1922.     {
  1923.       register int count;
  1924.  
  1925.       p = XSTRING (stuffstring)->data;
  1926.       count = XSTRING (stuffstring)->size;
  1927.       while (count-- > 0)
  1928.     stuff_char (*p++);
  1929.       stuff_char ('\n');
  1930.     }
  1931.   /* Anything we have read ahead, put back for the shell to read.  */
  1932.   while (kbd_count)
  1933.     {
  1934.       stuff_char (*kbd_ptr++);
  1935.       kbd_count--;
  1936.     }
  1937.   input_pending = 0;
  1938. #endif
  1939. #endif /* BSD and not BSD4_1 */
  1940. }
  1941.  
  1942. set_waiting_for_input (word_to_clear)
  1943.      long *word_to_clear;
  1944. {
  1945.   input_available_clear_word = word_to_clear;
  1946.  
  1947.   /* Tell interrupt_signal to throw back to read_command_char,  */
  1948.   waiting_for_input = 1;
  1949.  
  1950.   /* If interrupt_signal was called before and buffered a C-g,
  1951.      make it run again now, to avoid timing error.  */
  1952.   if (!NULL (Vquit_flag))
  1953.     quit_throw_to_read_command_char ();
  1954.  
  1955.   /* Tell alarm signal to echo right away */
  1956.   echo_now = 1;
  1957.  
  1958.   /* If alarm has gone off already, echo now.  */
  1959.   if (echo_flag)
  1960.     {
  1961.       echo ();
  1962.       echo_flag = 0;
  1963.     }
  1964. }
  1965.  
  1966. clear_waiting_for_input ()
  1967. {
  1968.   /* Tell interrupt_signal not to throw back to read_command_char,  */
  1969.   waiting_for_input = 0;
  1970.   echo_now = 0;
  1971.   input_available_clear_word = 0;
  1972. }
  1973.  
  1974. /* This routine is called at interrupt level in response to C-G.
  1975.  If interrupt_input, this is the handler for SIGINT.
  1976.  Otherwise, it is called from kbd_buffer_store_char,
  1977.  in handling SIGIO or SIGTINT.
  1978.  
  1979.  If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
  1980.  immediately throw back to read_command_char.
  1981.  
  1982.  Otherwise it sets the Lisp variable  quit-flag  not-nil.
  1983.  This causes  eval  to throw, when it gets a chance.
  1984.  If  quit-flag  is already non-nil, it stops the job right away.  */
  1985.  
  1986. interrupt_signal ()
  1987. {
  1988.   char c;
  1989.   /* Must preserve main program's value of errno.  */
  1990.   int old_errno = errno;
  1991.   extern Lisp_Object Vwindow_system;
  1992.  
  1993. #ifdef USG
  1994.   /* USG systems forget handlers when they are used;
  1995.      must reestablish each time */
  1996.   signal (SIGINT, interrupt_signal);
  1997.   signal (SIGQUIT, interrupt_signal);
  1998. #endif /* USG */
  1999.  
  2000.   cancel_echoing ();
  2001.  
  2002.   if (!NULL (Vquit_flag) && NULL (Vwindow_system))
  2003.     {
  2004.       fflush (stdout);
  2005.       reset_sys_modes ();
  2006.       sigfree ();
  2007. #ifdef SIGTSTP            /* Support possible in later USG versions */
  2008. /*
  2009.  * On systems which can suspend the current process and return to the original
  2010.  * shell, this command causes the user to end up back at the shell.
  2011.  * The "Auto-save" and "Abort" questions are not asked until
  2012.  * the user elects to return to emacs, at which point he can save the current
  2013.  * job and either dump core or continue.
  2014.  */
  2015.       sys_suspend ();
  2016. #else
  2017. #ifdef VMS
  2018.       if (sys_suspend () == -1)
  2019.     {
  2020.       printf ("Not running as a subprocess;\n");
  2021.       printf ("you can continue or abort.\n");
  2022.     }
  2023. #else /* not VMS */
  2024.       /* Perhaps should really fork an inferior shell?
  2025.      But that would not provide any way to get back
  2026.      to the original shell, ever.  */
  2027.       printf ("No support for stopping a process on this operating system;\n");
  2028.       printf ("you can continue or abort.\n");
  2029. #endif /* not VMS */
  2030. #endif /* not SIGTSTP */
  2031.       printf ("Auto-save? (y or n) ");
  2032.       fflush (stdout);
  2033.       if (((c = getchar ()) & ~040) == 'Y')
  2034.     Fdo_auto_save (Qnil);
  2035.       while (c != '\n') c = getchar ();
  2036. #ifdef VMS
  2037.       printf ("Abort (and enter debugger)? (y or n) ");
  2038. #else /* not VMS */
  2039.       printf ("Abort (and dump core)? (y or n) ");
  2040. #endif /* not VMS */
  2041.       fflush (stdout);
  2042.       if (((c = getchar ()) & ~040) == 'Y')
  2043.     abort ();
  2044.       while (c != '\n') c = getchar ();
  2045.       printf ("Continuing...\n");
  2046.       fflush (stdout);
  2047.       init_sys_modes ();
  2048.     }
  2049.   else
  2050.     {
  2051.       /* If executing a function that wants to be interrupted out of
  2052.          and the user has not deferred quitting by binding `inhibit-quit'
  2053.          then quit right away.  */
  2054.       if (immediate_quit && NULL (Vinhibit_quit))
  2055.     {
  2056.       immediate_quit = 0;
  2057.           sigfree ();
  2058.       Fsignal (Qquit, Qnil);
  2059.     }
  2060.       else
  2061.     /* Else request quit when it's safe */
  2062.     Vquit_flag = Qt;
  2063.     }
  2064.  
  2065.   if (waiting_for_input && !echoing)
  2066.     quit_throw_to_read_command_char ();
  2067.  
  2068.   errno = old_errno;
  2069. }
  2070.  
  2071. /* Handle a C-g by making read_command_char return C-g.  */
  2072.  
  2073. quit_throw_to_read_command_char ()
  2074. {
  2075.   quit_error_check ();
  2076.   sigfree ();
  2077.   /* Prevent another signal from doing this before we finish.  */
  2078.   waiting_for_input = 0;
  2079.   input_pending = 0;
  2080.   unread_command_char = -1;
  2081. #ifdef POLL_FOR_INPUT
  2082.   if (poll_suppress_count != 1)
  2083.     abort ();
  2084. #endif
  2085.   _longjmp (getcjmp, 1);
  2086. }
  2087.  
  2088. DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 2, 3, 0,
  2089.   "Set mode of reading keyboard input.\n\
  2090. First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
  2091. Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
  2092.  (no effect except in CBREAK mode).\n\
  2093. Optional third arg non-nil specifies character to use for quitting.\n\n\
  2094. Note that the arguments will change incompatibly in version 19.")
  2095.   (interrupt, flow, quit)
  2096.      Lisp_Object interrupt, flow, quit;
  2097. {
  2098.   reset_sys_modes ();
  2099. #ifdef SIGIO
  2100. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2101. #ifdef NO_SOCK_SIGIO
  2102.   if (read_socket_hook)
  2103.     interrupt_input = 0;    /* No interrupts if reading from a socket.  */
  2104.   else
  2105. #endif /* NO_SOCK_SIGIO */
  2106.     interrupt_input = !NULL (interrupt);
  2107. #else /* not SIGIO */
  2108.   interrupt_input = 0;
  2109. #endif /* not SIGIO */
  2110.   flow_control = !NULL (flow);
  2111.   if (!NULL (quit))
  2112.     {
  2113.       CHECK_NUMBER (quit, 2);
  2114.       quit_char = XINT (quit);
  2115.       /* Don't let this value be out of range.  */
  2116.       quit_char &= (meta_key ? 0377 : 0177);
  2117.     }
  2118.   init_sys_modes ();
  2119.   return Qnil;
  2120. }
  2121.  
  2122. init_keyboard ()
  2123. {
  2124.   this_command_keys_size = 40;
  2125.   this_command_keys = (unsigned char *) xmalloc (40);
  2126.  
  2127.   command_loop_level = -1;    /* Correct, before outermost invocation.  */
  2128.   quit_char = Ctl ('G');
  2129.   immediate_quit = 0;
  2130.   unread_command_char = -1;
  2131.   recent_keys_index = 0;
  2132.   total_keys = 0;
  2133.   kbd_count = 0;
  2134.   kbd_ptr = kbd_buffer;
  2135.   input_pending = 0;
  2136.   force_input = 0;
  2137.   if (!noninteractive)
  2138.     {
  2139.       signal (SIGINT, interrupt_signal);
  2140. #ifdef HAVE_TERMIO
  2141.       /* On  systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
  2142.      and we can't tell which one it will give us.  */
  2143.       signal (SIGQUIT, interrupt_signal);
  2144. #endif /* HAVE_TERMIO */
  2145. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2146. #ifdef SIGIO
  2147.       signal (SIGIO, input_available_signal);
  2148. #endif /* SIGIO */
  2149.     }
  2150.  
  2151. /* Use interrupt input by default, if it works and noninterrupt input
  2152.    has deficiencies.  */
  2153.  
  2154. #ifdef INTERRUPT_INPUT
  2155.   interrupt_input = 1;
  2156. #else
  2157.   interrupt_input = 0;
  2158. #endif
  2159.  
  2160.   sigfree ();
  2161.   dribble = 0;
  2162.  
  2163.   if (keyboard_init_hook)
  2164.     (*keyboard_init_hook) ();
  2165.  
  2166.   poll_suppress_count = 1;
  2167. #ifdef POLL_FOR_INPUT
  2168.   start_polling ();
  2169. #endif
  2170. }
  2171.  
  2172. syms_of_keyboard ()
  2173. {
  2174.   Qself_insert_command = intern ("self-insert-command");
  2175.   staticpro (&Qself_insert_command);
  2176.  
  2177.   Qforward_char = intern ("forward-char");
  2178.   staticpro (&Qforward_char);
  2179.  
  2180.   Qbackward_char = intern ("backward-char");
  2181.   staticpro (&Qbackward_char);
  2182.  
  2183.   Qdisabled = intern ("disabled");
  2184.   staticpro (&Qdisabled);
  2185.  
  2186.   defsubr (&Sread_key_sequence);
  2187.   defsubr (&Srecursive_edit);
  2188.   defsubr (&Sinput_pending_p);
  2189.   defsubr (&Scommand_execute);
  2190.   defsubr (&Srecent_keys);
  2191.   defsubr (&Sthis_command_keys);
  2192.   defsubr (&Ssuspend_emacs);
  2193.   defsubr (&Sabort_recursive_edit);
  2194.   defsubr (&Sexit_recursive_edit);
  2195.   defsubr (&Srecursion_depth);
  2196.   defsubr (&Stop_level);
  2197.   defsubr (&Sdiscard_input);
  2198.   defsubr (&Sopen_dribble_file);
  2199.   defsubr (&Sset_input_mode);
  2200.   defsubr (&Sexecute_extended_command);
  2201.  
  2202.   DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
  2203.     "Value is called instead of any command that is disabled\n\
  2204. \(has a non-nil  disabled  property).");
  2205.  
  2206.   DEFVAR_BOOL ("meta-flag", &meta_key,
  2207.     "*Non-nil means treat 0200 bit in terminal input as Meta bit.");
  2208.  
  2209.   DEFVAR_INT ("last-command-char", &last_command_char,
  2210.     "Last terminal input character that was part of a command, as an integer.");
  2211.  
  2212.   DEFVAR_INT ("last-input-char", &last_input_char,
  2213.     "Last terminal input character, as an integer.");
  2214.  
  2215.   DEFVAR_INT ("unread-command-char", &unread_command_char,
  2216.     "Character to be read as next input from command input stream, or -1 if none.");
  2217.  
  2218.   DEFVAR_INT ("meta-prefix-char", &meta_prefix_char,
  2219.     "Meta-prefix character code.  Meta-foo as command input\n\
  2220. turns into this character followed by foo.");
  2221.   meta_prefix_char = 033;
  2222.  
  2223.   DEFVAR_LISP ("last-command", &last_command,
  2224.     "The last command executed.  Normally a symbol with a function definition,\n\
  2225. but can be whatever was found in the keymap, or whatever the variable\n\
  2226. `this-command' was set to by that command.");
  2227.   last_command = Qnil;
  2228.  
  2229.   DEFVAR_LISP ("this-command", &this_command,
  2230.     "The command now being executed.\n\
  2231. The command can set this variable; whatever is put here\n\
  2232. will be in  last-command  during the following command.");
  2233.   this_command = Qnil;
  2234.  
  2235.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  2236.     "*Number of keyboard input characters between auto-saves.\n\
  2237. Zero means disable autosaving.");
  2238.   auto_save_interval = 300;
  2239.  
  2240.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  2241.     "*Nonzero means echo unfinished commands after this many seconds of pause.");
  2242.   echo_keystrokes = 1;
  2243.  
  2244.   DEFVAR_INT ("polling-period", &polling_period,
  2245.     "*Interval between polling for input during Lisp execution.\n\
  2246. The reason for polling is to make C-g work to stop a running program.\n\
  2247. Polling is needed only when using X windows and SIGIO does not work.\n\
  2248. Polling is automatically disabled in all other cases.");
  2249.   polling_period = 2;
  2250.  
  2251.   DEFVAR_INT ("help-char", &help_char,
  2252.     "Character to recognize as meaning Help.\n\
  2253. When it is read, do (eval help-form), and display result if it's a string.\n\
  2254. If help-form's value is nil, this char can be read normally.");
  2255.   help_char = Ctl ('H');
  2256.  
  2257.   DEFVAR_LISP ("help-form", &Vhelp_form,
  2258.     "Form to execute when character help-char is read.\n\
  2259. If the form returns a string, that string is displayed.\n\
  2260. If help-form is nil, the help char is not recognized.");
  2261.   Vhelp_form = Qnil;
  2262.   
  2263.   DEFVAR_LISP ("top-level", &Vtop_level,
  2264.     "Form to evaluate when Emacs starts up.\n\
  2265. Useful to set before you dump a modified Emacs.");
  2266.   Vtop_level = Qnil;
  2267.  
  2268.   DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
  2269.     "String used as translate table for keyboard input, or nil.\n\
  2270. Each character is looked up in this string and the contents used instead.\n\
  2271. If string is of length N, character codes N and up are untranslated."); 
  2272.   Vkeyboard_translate_table = Qnil;
  2273. }
  2274.  
  2275. keys_of_keyboard ()
  2276. {
  2277.   ndefkey (Vglobal_map, Ctl ('Z'), "suspend-emacs");
  2278.   ndefkey (Vctl_x_map, Ctl ('Z'), "suspend-emacs");
  2279.   ndefkey (Vesc_map, Ctl ('C'), "exit-recursive-edit");
  2280.   ndefkey (Vglobal_map, Ctl (']'), "abort-recursive-edit");
  2281.   ndefkey (Vesc_map, 'x', "execute-extended-command");
  2282. }
  2283.